package com.flute.framework.search.result.xmlresult;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import junit.framework.TestCase;

import org.apache.commons.io.IOUtils;

public class XMLParserHelper extends TestCase {

	public void test() {
		FileReader inSource = null;
		BufferedReader sourceBr = null;
		FileReader inAim = null;
		BufferedReader aimBr = null;
		try {
			Scanner scanner = new Scanner(System.in);
			System.out.println("Xpath&valueFileName :");
			String sourceFileName = scanner.nextLine();
			System.out.println("aimXmlFileName :");
			String aimFileName = scanner.nextLine();
			File sourceFile = new File(sourceFileName);
			File aimFile = new File(aimFileName);
			if (!sourceFile.exists()) {
				System.out.println("source file is not exists");
				return;
			}
			if (!aimFile.exists()) {
				System.out.println("aim file is not exists");
				return;
			}
			inSource = new FileReader(sourceFile);
			sourceBr = new BufferedReader(inSource);
			inAim = new FileReader(aimFile);
			aimBr = new BufferedReader(inAim);
			String lineStr = null;
			StringBuilder aimBuilder = new StringBuilder();
			List<String> pathList = new ArrayList<String>();
			List<String> valueList = new ArrayList<String>();
			int count = 0;
			while ((lineStr = sourceBr.readLine()) != null) {
				String xmlpath[] = lineStr.split("[ ]+");
				count++;
				if (xmlpath != null && xmlpath.length <= 2
						&& xmlpath.length >= 1) {
					if (xmlpath.length == 2) {
						pathList.add(xmlpath[0]);
						valueList.add(xmlpath[1]);
					} else {
						pathList.add(xmlpath[0]);
						valueList.add(null);
					}
				} else {
					System.out.println("error data on parser Source ,at line :"
							+ count + " \n data: " + lineStr);
				}
			}
			String[] paths = new String[pathList.size()];
			String[] values = new String[valueList.size()];
			pathList.toArray(paths);
			valueList.toArray(values);
			while ((lineStr = aimBr.readLine()) != null) {
				aimBuilder.append(lineStr);
			}
			long startTime = System.currentTimeMillis();
			String xmlStr = XmlParser.getInstance().parser(paths, values);
			long parserTime = System.currentTimeMillis() - startTime;
			if (compareTo(xmlStr, aimBuilder.toString())) {
				System.out.println("parser success");
			} else {
				System.out.println("parser error");
			}
			System.out.println("parser time :" + parserTime + " Millis");
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			IOUtils.closeQuietly(inSource);
			IOUtils.closeQuietly(sourceBr);
			IOUtils.closeQuietly(inAim);
			IOUtils.closeQuietly(aimBr);
		}
	}

	public void testByInput() {
		InputStream is = System.in;
		testGetNode(is);
	}

	public void testByFile() {

		Scanner scanner = new Scanner(System.in);
		String fileName = scanner.nextLine();
		File file = new File(fileName);
		InputStream is = null;
		try {
			if (file.exists()) {
				is = new FileInputStream(file);
				testGetNode(is);
			} else {
				throw new FileNotFoundException("file not found :"
						+ file.getAbsolutePath());
			}
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
			throw new RuntimeException(e);
		} finally {
			IOUtils.closeQuietly(is);
		}
	}

	private void testGetNode(InputStream is) {
		List<String> pathList = new ArrayList<String>();
		List<String> valueList = new ArrayList<String>();
		Scanner in = new Scanner(is);
		System.out.println("XPath\tValue");
		String lineStr = null;
		while (!(lineStr = in.nextLine()).equals("")) {
			String[] strs = lineStr.split("[ ]+");
			if (strs.length == 2) {
				pathList.add(strs[0]);
				valueList.add(strs[1]);
			} else {
				break;
			}
		}
		String[] paths = new String[pathList.size()];
		pathList.toArray(paths);
		String[] values = new String[valueList.size()];
		valueList.toArray(values);
		Node node = XmlParser.getInstance().getNode(paths, null, values);
		System.out.println(node);
	}

	private static boolean compareTo(String sourceStr, String aimStr) {
		StringBuilder source = new StringBuilder();
		StringBuilder aim = new StringBuilder();
		for (int i = 0; i < sourceStr.length(); i++) {
			if (sourceStr.charAt(i) != ' ' && sourceStr.charAt(i) != '　') {
				source.append(sourceStr.charAt(i));
			}
		}
		for (int i = 0; i < aimStr.length(); i++) {
			if (aimStr.charAt(i) != ' ' && aimStr.charAt(i) != '　') {
				aim.append(aimStr.charAt(i));
			}
		}
		System.out.println(source.toString());
		System.out.println(aim.toString());
		return aim.toString().equals(source.toString());
	}

	public void toTestCode() {
		FileReader in = null;
		BufferedReader sourceBr = null;
		try {
			File sourceFile = new File(Constants.HOME_XWW_PATH);
			if (!sourceFile.exists()) {
				System.out.println("source file is not exists");
				return;
			}

			in = new FileReader(sourceFile);
			sourceBr = new BufferedReader(in);
			String lineStr = null;
			List<String> pathList = new ArrayList<String>();
			List<String> valueList = new ArrayList<String>();
			int count = 0;
			while ((lineStr = sourceBr.readLine()) != null) {
				String xmlpath[] = lineStr.split("[ ]+");
				count++;
				if (xmlpath != null && xmlpath.length <= 2
						&& xmlpath.length >= 1) {
					if (xmlpath.length == 2) {
						pathList.add(xmlpath[0]);
						valueList.add(xmlpath[1]);
					} else {
						pathList.add(xmlpath[0]);
						valueList.add(null);
					}
				} else {
					System.out.println("error data on parser Source ,at line :"
							+ count + " \n data: " + lineStr);
				}
			}
			System.out.println("{");
			for (int i = 0; i < pathList.size(); i++) {
				System.out.println("\"" + pathList.get(i) + "\""
						+ (i < pathList.size() - 1 ? "," : ""));
			}
			System.out.println("};");
			System.out.println("{");
			for (int i = 0; i < valueList.size(); i++) {
				if (valueList.get(i) != null) {
					System.out.println("\"" + valueList.get(i) + "\""
							+ (i < valueList.size() - 1 ? "," : ""));
				} else {
					System.out.println(valueList.get(i)
							+ (i < valueList.size() - 1 ? "," : ""));
				}
			}
			System.out.println("};");
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			IOUtils.closeQuietly(in);
			IOUtils.closeQuietly(sourceBr);
		}

	}
}
